package org.classdef.scan.print;

import java.io.PrintStream;
import java.util.LinkedList;

import org.classdef.analyzer.info.ClassDef;

public class ConsoleResultPrinter implements IResultPrinter {

	protected PrintStream out;
	protected IPrintFormater formater;
	
	protected LinkedList<String> msgList;
	
	public ConsoleResultPrinter(PrintStream out) {
		this(out, null);
	}
	
	public ConsoleResultPrinter(PrintStream out, IPrintFormater formater) {
		this.out = out;
		this.formater = formater;
	}

	@Override
	public void start() {
		msgList = new LinkedList<String>();
	}

	@Override
	public void stop() {
		out.println("============================================================================");
		out.println("结果：");
		for (String msg : msgList) {
			out.println(msg + "\r\n");
		}
	}

	@Override
	public void print(ClassDef info) {
		String msg = getOut(info);
		if(msg != null && !"".equals(msg)){
			msgList.add(msg);
		}
		
	}
	
	private String getOut(ClassDef info){
		if(this.formater != null){
			return this.formater.format(info);
		}
		return info.toString();
	}

}
